#!/bin/sh
# Install script for burncd.

# Copyright 2017-2020 orbea
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

IFS=' 	
'
\unset -f command printf unalias : 2>/dev/null
\unalias -a 2>/dev/null
PATH="$(command -p getconf PATH):$PATH"
LC_ALL=C; export LC_ALL

set -euf

die () {
  ret="$1"; shift
  case "$ret" in
    : ) printf %s\\n "$@" >&2; return 0 ;;
    0 ) printf %s\\n "$@" ;;
    * ) printf %s\\n "$@" >&2 ;;
  esac
  exit "$ret"
}

case "${1:-}" in
  install ) INSTALL=1 ;;
  uninstall ) INSTALL=0 ;;
  '' ) die 1 'No install target.' ;;
  * ) die 1 "No rule to install target '$1'." ;;
esac

cmd () {
  print=
  for command do
    case "$command" in
      -- ) : ;;
      */* ) print="${print} '$command'" ;;
      * ) print="${print} $command" ;;
    esac
  done
  printf " %s\\n" "$print"
  command -p "$@"
}

is_absolute () { case "$1" in /*) return 0 ;; *) return 1 ;; esac; }

PREFIX=; BINDIR=; DATADIR=; DATAROOTDIR=; DOCDIR=; MANDIR=; SYSCONFDIR=
DESTDIR=

CWD="$(pwd)"

if [ "$0" != "${0%/*}" ]; then
  SOURCE_DIR="$(cd -- "${0%/*}/" && pwd -P)"
else
  SOURCE_DIR="$(pwd -P)"
fi

PRGNAM=burncd
VERSION=0.0

shift

install_args=

while [ $# -gt 0 ]; do
  arg="$1"; shift
  case "${arg%%=*}" in
    DESTDIR|PREFIX )
      eval "${arg%%=*}=\"\${arg#*=}\""
    ;;
    BINDIR|DATADIR|DATAROOTDIR|DOCDIR|MANDIR|SYSCONFDIR )
      install_args=1
      eval "${arg%%=*}=\"\${arg#*=}\""
    ;;
    * )
      die : "WARNING: Variable '${arg%%=*}' was not used by $PRGNAM."
    ;;
  esac
done

PREFIX="${PREFIX:-/usr/local}"
BINDIR="${BINDIR:-${PREFIX}/bin}"
DOCDIR="${DOCDIR:-${PREFIX}/share/doc}/$PRGNAM-$VERSION"
MANDIR="${MANDIR:-${PREFIX}/share/man}"
DATAROOTDIR="${DATAROOTDIR:-${PREFIX}/share}"
DATADIR="${DATADIR:-${DATAROOTDIR}}/$PRGNAM"
SYSCONFDIR="${SYSCONFDIR:-${PREFIX}/etc}"
DESTDIR="${DESTDIR:-}"

is_absolute "$PREFIX" || PREFIX="$CWD/$PREFIX"

if [ -n "$install_args" ]; then
  for i in \$BINDIR \$DATADIR \$DATAROOTDIR \$DOCDIR \$MANDIR \$SYSCONFDIR; do
    eval "dest=\"$i\""
    is_absolute "${dest:?}" || eval "${i#*$}=\"\${PREFIX}/$i\""
  done
fi

case $INSTALL in
  1 )
    printf %s\\n 'Installing...'
    cmd mkdir -p -- "${DESTDIR}${BINDIR}"
    cmd mkdir -p -- "${DESTDIR}${DOCDIR}"
    cmd mkdir -p -- "${DESTDIR}${MANDIR}/man1"
    cmd mkdir -p -- "${DESTDIR}${SYSCONFDIR}"
    cmd rm -f -- "${DESTDIR}${BINDIR}/$PRGNAM"
    cmd cp -- "${SOURCE_DIR}/$PRGNAM" "${DESTDIR}${BINDIR}"
    cmd rm -f -- "${DESTDIR}${MANDIR}/man1/$PRGNAM.1"
    cmd cp -- "${SOURCE_DIR}/$PRGNAM.1" "${DESTDIR}${MANDIR}/man1"
    cmd rm -f -- "${DESTDIR}${DOCDIR}/LICENSE"
    cmd rm -f -- "${DESTDIR}${SYSCONFDIR}/$PRGNAM.conf"
    cmd cp -- "${SOURCE_DIR}/$PRGNAM.conf" "${DESTDIR}${SYSCONFDIR}"
    cmd cp -- "${SOURCE_DIR}"/LICENSE "${DESTDIR}${DOCDIR}"
    cmd rm -f -- "${DESTDIR}${DOCDIR}/README"
    cmd cp -- "${SOURCE_DIR}"/README "${DESTDIR}${DOCDIR}"
    cmd chmod 0755 -- "${DESTDIR}${BINDIR}/$PRGNAM"
  ;;
  0 )
    printf %s\\n 'Uninstalling...'
    cmd rm -rf -- "${DESTDIR}${DOCDIR}"
    cmd rm -f -- "${DESTDIR}${BINDIR}/$PRGNAM"
    cmd rm -f -- "${DESTDIR}${MANDIR}/man1/$PRGNAM.1"
    cmd rm -f -- "${DESTDIR}${SYSCONFDIR}/$PRGNAM.conf"
  ;;
esac

exit 0
